//
//  HomeDetailsViewController.swift
//  LiuheCommunity
//
//  Created by 罗恒 on 2018/1/24.
//  Copyright © 2018年 luo. All rights reserved.
//

import UIKit

class HomeDetailsViewController: UIViewController,UIScrollViewDelegate {
    var acId:String?
    var scrollView = UIScrollView()
    
    var imageH:CGFloat = 200.0
    var price:UILabel?
    var state:String?
    var state2:String?
    var btn:UIButton?
    var peopleView:UIView?
    var peopleLable:UILabel?
    var upper:UILabel?
    var npeople:UILabel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "活动详情"
        view.backgroundColor = UIColor.white
        
        self.tabBarController?.hidesBottomBarWhenPushed = true;
        
        self.tabBarController?.tabBar.isHidden = true//隐藏tabbar
        
        self.automaticallyAdjustsScrollViewInsets = false//移除隐藏后留下的空白
        
        
        scrollView.frame = CGRect(x: 0, y: 0, width: w, height: h)
        scrollView.contentSize = CGSize(width: w, height: h )
        scrollView.delegate = self
        self.view.addSubview(scrollView)
        
        let btnView = UIView.init(frame: CGRect(x: 0, y: h-50, width: w, height: 50))
        btnView.backgroundColor = UIColor.red    
        self.view.addSubview(btnView)
        
        price = UILabel.init(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
        price?.textAlignment = NSTextAlignment.center
        price?.backgroundColor = UIColor.white
        price?.textColor = UIColor.red
        price?.font = UIFont.systemFont(ofSize: 20.0)
        btnView.addSubview(price!)
        
        btn = UIButton.init(frame: CGRect(x: 100, y: 0, width: w-100, height: 50))
        btn?.backgroundColor = UIColor.red
        btn?.setTitle("立即报名", for: .normal)
        btn?.titleLabel?.font = UIFont.systemFont(ofSize: 20.0)
        btn?.titleLabel?.tintColor = UIColor.white
        btnView.addSubview(btn!)
        
        btn?.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(btnClick)))
        
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
 
        self.tabBarController?.hidesBottomBarWhenPushed = true;
        
        self.tabBarController?.tabBar.isHidden = true//隐藏tabbar
        
        self.automaticallyAdjustsScrollViewInsets = false//移除隐藏后留下的空白
        
        WebService.shareInstance.activityDetail(activityId: self.acId) { (isSuccess, par) in
            let w = self.view.frame.size.width
            let share = ShareInfo.shareInstance
            
                        
            if isSuccess == true{
                let model = ShareInfo.shareInstance.modAc
                self.price?.text = model?.discountedPrice
                self.state = model?.state
                let picURL = NSURL.init(string: (model?.imgMainUrl)!)
                let imageV = UIImageView.init(frame:  CGRect(x: 0, y: 64
                    , width: Int(w), height: Int(self.imageH) ))
                imageV.sd_setImage(with: picURL! as URL)
                self.scrollView.addSubview(imageV)
                
                self.peopleView = UIView.init(frame: CGRect(x: w-130, y: self.imageH-30, width: 130, height: 30))
                //                设置背景透明,子控件不影响
                self.peopleView?.backgroundColor = UIColor.init(red: 120 / 255.0, green: 122 / 255.0, blue: 145 / 255.0, alpha: 0.5)
                imageV.addSubview(self.peopleView!)
                
                self.peopleLable = UILabel.init(frame: CGRect(x: 10, y: 0, width: 60, height: 30))
                self.peopleLable?.text = "已报名:"
                self.peopleLable?.font = UIFont.boldSystemFont(ofSize: 15.0)
                self.peopleLable?.textColor = UIColor.white
                self.peopleView?.addSubview(self.peopleLable!)
                
                self.upper = UILabel.init(frame: CGRect(x: 90, y: 0, width: 60, height: 30))
                self.upper?.text = model?.upperLimit
                self.upper?.textColor = UIColor.white
                let fuhao = UILabel.init(frame: CGRect(x: 80, y: 0, width: 10, height: 30))
                fuhao.text = "/"
                fuhao.textColor = UIColor.white
                self.npeople = UILabel.init(frame: CGRect(x: 65, y: 0, width: 60, height: 30))
                self.npeople?.textColor = UIColor.white
                let npeo = model!.nPeople
                let str = NSString.init(format: "%d", npeo!)
                self.npeople?.text = str as String
                self.peopleView?.addSubview(self.npeople!)
                self.peopleView?.addSubview(fuhao)
                self.peopleView?.addSubview(self.upper!)
                
                
                self.scrollView.contentSize = CGSize(width: w, height: (1.0 + CGFloat(share.imgsArray.count) ) * self.imageH+50+64)
                if (share.imgsArray != nil && share.imgsArray.count > 0){
                for n in 0...share.imgsArray!.count - 1{
                    let models = share.imgsArray[n] as? ImageModel
                    let url = NSURL.init(string: (models?.url!)!)
                    let imageVV = UIImageView.init(frame: CGRect(x: 0, y: self.imageH * CGFloat(n + 1)+64
                        , width: CGFloat(w), height: self.imageH ))
                        imageVV.sd_setImage(with: url! as URL)
                    self.scrollView.addSubview(imageVV)
                    }
                }
                if (self.state == "1"){
                    self.btn?.isUserInteractionEnabled = false
                    self.btn?.setTitle("报名已关闭", for: .normal)
                    self.btn?.backgroundColor = UIColor.gray
                }else{
                    
                }
            }else{
                
                let a = ""
                
                MBProgressHUD.showToast(to: self.view, withText: a)
                
            }
        }
        
        WebService.shareInstance.stateInfo(activityId: self.acId){(isSuccess,par)in
            if isSuccess == true{
                let share = ShareInfo.shareInstance
                let model1 = share.stateInfo
                self.state2 = model1?.state2
            }else{
                
                let a = ""
                
                MBProgressHUD.showToast(to: self.view, withText: a)
                
            }
            
        }
        
    }
    
    @objc func btnClick(){
        let model1 = ShareInfo.shareInstance.modAc
        if (self.state2 == "0"){
            let vc = PayViewController()
            vc.acId = model1?.activityId
            self.navigationController?.pushViewController(vc, animated: true)
        }else if (self.state2 == "1"){
            let alert = UIAlertView.init(title: "", message: "活动已报名", delegate:self, cancelButtonTitle: "确定")
            alert.show()
        }else if(self.state2 == "2"){
            let alert = UIAlertView.init(title: "", message: "活动进行中", delegate:self, cancelButtonTitle: "确定")
            alert.show()
        }else if(self.state2 == "3"){
            let alert = UIAlertView.init(title: "", message: "活动已结束", delegate:self, cancelButtonTitle: "确定")
            alert.show()
        }
    }
}

